#ifndef __BSP_TOOL_H__
#define __BSP_TOOL_H__

#include "main.h"
#include <stdint.h>

#define CRC_CHECK_TOOL    (1)
#define TIM_DELAY_TOOL    (1)
#define UART_PRINTF_TOOL  (0)
#define USE_HAL_LIB		  	(1)
#define USE_SEGGER_RTT		(1)


#if TIM_DELAY_TOOL
#define MyToolTimer (htim14)
#endif

#if USE_SEGGER_RTT
#define  SEGGER_UP_BUFF_SIZE		(256)
#define  SEGGER_DOWN_BUFF_SIZE	(256)
#define  SEGGER_INDEX_LINE			(0)
#include "SEGGER_RTT.h"

#define SEGGER_OUT(buff,size)\
				SEGGER_RTT_Write(SEGGER_INDEX_LINE, buff, size)
				
#define SEGGER_LOG(fmt, ...)\
				SEGGER_RTT_printf(SEGGER_INDEX_LINE, "[%s:%d] " fmt "\n\r", __FILE__, __LINE__, ##__VA_ARGS__)
				
#define SEGGER_READ_CHAR(ch)\
				SEGGER_RTT_Read(SEGGER_INDEX_LINE, &ch, 1)
				
#define SEGGER_READ_BUFF(buff, size)\
				SEGGER_RTT_Read(SEGGER_INDEX_LINE, buff, size)
#endif



#define UINT16_TO_UINT8(data8,data16) (data8[0]) = ((data16)>>8)&0xFF,\
                                      (data8[1]) = ((data16))&0xFF
                                      
#define UINT32_TO_UINT8(data8,data32) (data8[0]) = ((data16)>>24)&0xFF,\
                                      (data8[1]) = ((data16)>>16)&0xFF,\
                                      (data8[2]) = ((data16)>>8)&0xFF,\
                                      (data8[3]) = ((data16))&0xFF,\

                                      
#define PARMA_LIMIT(DATA,MAX,MIN) ((DATA)>(MAX)?0:(DATA)<(MIN)?0:1)

#define PARMA_LIMIT_BETWEEN(DATA,BASE,RANGE) PARMA_LIMIT((DATA),(BASE+RANGE),(BASE-RANGE))


#if CRC_CHECK_TOOL
uint8_t crc_8(uint8_t data[],uint16_t len);
int16_t complement_conversion_int16(int16_t complement_data);
#endif

#if TIM_DELAY_TOOL
void  				    delay_us(uint32_t time);
void 					tim_countUs_start(void);
uint32_t 			    tim_countUs_get(void);
void 					tim_countUs_stop(void);
#endif

#if USE_HAL_LIB
uint8_t tim_checkTimeout(uint32_t startTick, uint32_t timeout);
#endif

#if USE_SEGGER_RTT
void SeggerInit(void);
#endif

#endif
